

package com.hex.ds.code.detection.pack.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hex.ds.code.detection.common.entity.ScriptPackScanResultEntity;
import com.hex.ds.code.detection.pack.service.IScriptPackScanResultService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.nio.file.Files;
import java.util.List;


/**
 * 脚本扫描结果
 *
 * @author Wang zhihao
 * @date 2023-10-07 17:48:50
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/api/scriptPack/scanResult" )
public class ScriptPackScanResultController {

    private final IScriptPackScanResultService scriptPackScanResultService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param scriptPackScanResult 脚本扫描结果
     * WrapperEnhancer.like(scriptPackScanResult); //自动拼接存在值sql的方法
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public ResponseEntity<Object> getScriptPackScanResultPage(Page page, ScriptPackScanResultEntity scriptPackScanResult) {
        return new ResponseEntity<>(scriptPackScanResultService.queryList(page,scriptPackScanResult), HttpStatus.OK);
    }


    /**
     * 通过id查询脚本扫描结果
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public ResponseEntity<Object> getById(@PathVariable("id" ) String id) {
        return ResponseEntity.ok(scriptPackScanResultService.getById(id));
    }

    /**
     * 新增脚本扫描结果
     * @param scriptPackScanResult 脚本扫描结果
     * @return R
     */
    @ApiOperation(value = "新增脚本扫描结果", notes = "新增脚本扫描结果")
    @PostMapping
    public ResponseEntity<Object> save(@RequestBody ScriptPackScanResultEntity scriptPackScanResult) {
        return ResponseEntity.ok(scriptPackScanResultService.save(scriptPackScanResult));
    }

    /**
     * 修改脚本扫描结果
     * @param scriptPackScanResult 脚本扫描结果
     * @return R
     */
    @ApiOperation(value = "修改脚本扫描结果", notes = "修改脚本扫描结果")
    @PutMapping
    public ResponseEntity<Object> updateById(@RequestBody ScriptPackScanResultEntity scriptPackScanResult) {
        return ResponseEntity.ok(scriptPackScanResultService.updateById(scriptPackScanResult));
    }

    /**
     * 通过id删除脚本扫描结果
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除脚本扫描结果", notes = "通过id删除脚本扫描结果")
    @DeleteMapping("/{id}" )
    public ResponseEntity<Object> removeById(@PathVariable String id) {
        return ResponseEntity.ok(scriptPackScanResultService.removeById(id));
    }

    @ApiOperation("导出扫描结果清单")
    @GetMapping(value = "/download")
    public ResponseEntity<Resource> download(@RequestParam List<String> listId, @RequestParam String taskId) throws Exception {
        String downloadFile = scriptPackScanResultService.download(listId, taskId);
        byte[] bytes = Files.readAllBytes(new File(downloadFile).toPath());
        ByteArrayResource bar = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=文件.zip")
                .body(bar);
    }

    @ApiOperation(value = "通过id忽略告警", notes = "通过id忽略告警")
    @PutMapping("/{listId}/{id}" )
    public ResponseEntity<Object> ignoreById(@PathVariable("listId" ) String listId, @PathVariable("id" ) String id) {
        scriptPackScanResultService.ignoreAndRefreshList(listId, id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
